package com.atguigu.com.cart.controller;

import com.atguigu.com.cart.interceptor.CartInterceptor;
import com.atguigu.com.cart.service.CartService;
import com.atguigu.com.cart.vo.Cart;
import com.atguigu.com.cart.vo.CartItem;
import com.atguigu.com.cart.vo.UserInfoTo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutionException;

@Controller
public class CartController {

    @Autowired
    CartService cartService;

    @GetMapping("/cart/currentUserCartItem")
    @ResponseBody
    public List<CartItem>  getCurrentUserCartItem(@RequestParam("userId")Long userId){
        List<CartItem> userCartItem = cartService.getUserCartItem(userId);
        return userCartItem;

    }

    @GetMapping("/deleteItem")
    public String  deleteItem(@RequestParam("skuId") Long skuId){
        cartService.deleteItem(skuId);
        return  "redirect:http://cart.gulimall.com/cart.html";
    }
    /**
     * 修改购物车数量
     * @param skuId
     * @param num
     * @return
     */
    @GetMapping("/countItem")
    public String  countItem(@RequestParam("skuId") Long skuId,@RequestParam("num") Integer num){
        cartService.countItem(skuId,num);
        return  "redirect:http://cart.gulimall.com/cart.html";
    }

    /**
     * 修改勾选状态
     * @param skuId
     * @param check
     * @return
     */
    @GetMapping("/checkItem")
    public String  checkItem(@RequestParam("skuId") Long skuId,@RequestParam("check") Integer check){
        cartService.checkItem(skuId,check);
        return  "redirect:http://cart.gulimall.com/cart.html";
    }

    /**
     * 获取购物车的项目列表
     * @param session
     * @param model
     * @return
     * @throws ExecutionException
     * @throws InterruptedException
     */
    @GetMapping("cart.html")
    public String cartListPage(HttpSession session,Model model) throws ExecutionException, InterruptedException {
        Cart cart = cartService.getCart();
        model.addAttribute("cart",cart);
        return "cartList";
    }

    /**
     * 添加商品到购物车
     *
     * @return
     */
    @GetMapping("/addToCart")
    public String addToCart(@RequestParam("skuId") Long skuId, @RequestParam("number") Integer number, RedirectAttributes ra) throws ExecutionException, InterruptedException {
        cartService.addToCart(skuId, number);
        ra.addAttribute("skuId", skuId);
        return "redirect:http://cart.gulimall.com/addToCartSuccess.html";
    }
    /**
     * 查询商品  跳转到success.html
     *
     * @return
     */
    @GetMapping("/addToCartSuccess.html")
    public String addToCartSuccess(@RequestParam("skuId") Long skuId,Model model) {
        CartItem cartItem = cartService.getCartItem(skuId);
        model.addAttribute("item",cartItem);
        return "success";
    }
}


